package com.kecheng.action;

import com.kecheng.dao.CommonDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Kaoshi;
import com.kecheng.pojo.TestDetail;
import com.kecheng.pojo.TiMu;
import com.kecheng.pojo.TmOption;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台考试模块
 *
 * @author
 */

@Action(value = "kaoshi", results = {
        @Result(name = "kaoshiList", location = "/admin/kaoshi/kaoshiList.jsp"),
        @Result(name = "toDetail", location = "/admin/kaoshi/detail.jsp"),
        @Result(name = "redirectList", type = "redirect",location = "kaoshi!kaoshiList.action"),
})
@AllowedMethods(value = {("regex:.*")})
public class KaoshiAction extends BaseAction {

    @Autowired
    CommonDao dao;

    private Kaoshi kaoshi;
    private List<Integer> id;
    List<TestDetail>testDetails;

    private PageBean<Kaoshi> pageBean;

    /**
     * 考试列表
     * @return
     */
    public String kaoshiList(){
        if(kaoshi==null){
            kaoshi = new Kaoshi();
            kaoshi.setType(1);
        }
        pageBean = (PageBean<Kaoshi>) dao.findPageBeanByObj(kaoshi,pageBean);
        return "kaoshiList";
    }
    /**
     *考试详情页
     * @return
     */
    public String toDetail(){
        if(kaoshi!=null){
            //考试
            kaoshi = (Kaoshi)dao.findOne(kaoshi);
            TestDetail testDetail = new TestDetail();
            testDetail.setKaoshiId(kaoshi.getId());
            //考试详细
            testDetails = (List<TestDetail>)dao.find(testDetail);
            for (TestDetail td:testDetails){
                //题目
                TiMu tiMu = new TiMu();
                tiMu.setId(td.getTimuId());
                tiMu = (TiMu)dao.findOne(tiMu);
                td.setTiMu(tiMu);
                //选项
                TmOption tmOption = new TmOption();
                tmOption.setTmId(td.getTimuId());
                List<TmOption>tmOptions = (List<TmOption>)dao.find(tmOption);

                td.setTmOptions(tmOptions);
            }
        }
        return "toDetail";
    }

    /**
     * 更新或者添加
     */
    @Transactional(readOnly = false)
    public String save(){

        if(kaoshi.getId()!=null){
            dao.update(kaoshi);
        }else{

            dao.add(kaoshi);
        }
        return "redirectList";
    }

    /**
     *删除
     * @return
     */
    @Transactional(readOnly = false)
    public String delete(){
        if(kaoshi!=null)dao.delete(kaoshi);
        else if(id!=null){
            for(Integer i:id){
                kaoshi = new Kaoshi();
                kaoshi.setId(i);
                dao.delete(kaoshi);
            }
        }
        return "redirectList";
    }

    public Kaoshi getKaoshi() {
        return kaoshi;
    }

    public void setKaoshi(Kaoshi kaoshi) {
        this.kaoshi = kaoshi;
    }

    public List<Integer> getId() {
        return id;
    }

    public void setId(List<Integer> id) {
        this.id = id;
    }

    public PageBean<Kaoshi> getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean<Kaoshi> pageBean) {
        this.pageBean = pageBean;
    }

    public List<TestDetail> getTestDetails() {
        return testDetails;
    }

    public void setTestDetails(List<TestDetail> testDetails) {
        this.testDetails = testDetails;
    }
}
